package com.zuxia.impDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zuxia.iDao.InfocenterinfoDAOI;
import com.zuxia.po.Infocenterinfo;
import com.zuxia.po.Infotypeinfo;
import com.zuxia.po.Positioninfo;
import com.zuxia.po.Recruitinfo;
import com.zuxia.util.PageModel;

/**
 * A data access object (DAO) providing persistence and search support for
 * Infocenterinfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.zuxia.impDao.Infocenterinfo
 * @author MyEclipse Persistence Tools
 */

public class InfocenterinfoDAO extends HibernateDaoSupport implements InfocenterinfoDAOI {
	private static final Logger log = LoggerFactory
			.getLogger(InfocenterinfoDAO.class);
	// property constants
	public static final String ICITITLE = "icititle";
	public static final String ICICONTEXT = "icicontext";

	protected void initDao() {
		// do nothing
	}

	public void save(Infocenterinfo transientInstance) {
		log.debug("saving Infocenterinfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public Boolean delete(Infocenterinfo persistentInstance) {
		log.debug("deleting Infocenterinfo instance");
		try {
			
			SessionFactory factory = getHibernateTemplate().getSessionFactory();
			Session session = factory.getCurrentSession();
			session.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
		return true;
	}

	public Infocenterinfo findById(java.lang.Integer id) {
		log.debug("getting Infocenterinfo instance with id: " + id);
		try {
			Infocenterinfo instance = (Infocenterinfo) getHibernateTemplate()
					.get("com.zuxia.po.Infocenterinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Infocenterinfo instance) {
		log.debug("finding Infocenterinfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InfocenterinfoDAOI#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Infocenterinfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Infocenterinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InfocenterinfoDAOI#findByIcititle(java.lang.Object)
	 */
	public List findByIcititle(Object icititle) {
		return findByProperty(ICITITLE, icititle);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InfocenterinfoDAOI#findByIcicontext(java.lang.Object)
	 */
	public List findByIcicontext(Object icicontext) {
		return findByProperty(ICICONTEXT, icicontext);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InfocenterinfoDAOI#findAll()
	 */
	public List findAll() {
		log.debug("finding all Infocenterinfo instances");
		try {
			String queryString = "from Infocenterinfo";
			List list = getHibernateTemplate().find(queryString);
			return getHibernateTemplate().find(queryString);
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Infocenterinfo merge(Infocenterinfo detachedInstance) {
		log.debug("merging Infocenterinfo instance");
		try {
			Infocenterinfo result = (Infocenterinfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Infocenterinfo instance) {
		log.debug("attaching dirty Infocenterinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Infocenterinfo instance) {
		log.debug("attaching clean Infocenterinfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static InfocenterinfoDAOI getFromApplicationContext(
			ApplicationContext ctx) {
		return (InfocenterinfoDAOI) ctx.getBean("InfocenterinfoDAO");
	}
	
	//分页查询
	public List showPageModelList(final int pageNumber, final int limit,String icititle) {
		//final Object[] values
		log.debug("showPageModelList"); 
		final StringBuffer hql = new StringBuffer("from Infocenterinfo as a ");
		if(!icititle.equals("")) {
			hql.append("where icititle like '%"+icititle+"%'");
		}
		hql.append("order by icitime desc");
		System.out.println("-----------"+hql.toString());
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(org.hibernate.Session session)
					throws HibernateException, SQLException {
				 	Query query = session.createQuery(hql.toString());
				    //这个用于扩展多条件查询用到的
				   /* for (int i = 0; i < values.length; i++) {
				     query.setParameter(i, values[i]);
				    }*/
				    int p = pageNumber;
				    int l = limit;
				    int a = (p-1)*l;
				    List result = query.setFirstResult(a)
				        .setMaxResults(l)
				        .list();
				    /*List result = query
					        .list();*/
				    System.out.println("需求dao中查询到了数据"+result);
				    return result;
			}
		});
		
		return list;
	}
	
	//获得新闻纪录数的方法
	public int queryCout(String serachstuName) {
		log.debug("sel ALL Staffindo For PageCount");
		StringBuffer hql = new StringBuffer("select count(*) from Infocenterinfo as a ");
		if(serachstuName!=null) {
			hql.append("where icititle like "+"'%"+serachstuName+"%'");
		}
		System.out.println(hql);
		long id=(Long) getHibernateTemplate().iterate(hql.toString()).next();
		return (int)id;
	}

	public Boolean xzCut(int cid) {
		Boolean b=true;
		Infocenterinfo infocenterinfo = getHibernateTemplate().get(Infocenterinfo.class,cid);
		System.out.println(infocenterinfo.getIciid());
		log.debug("deleting Infocenterinfo instance");
		try {
			infocenterinfo.getInfotypeinfo().getInfocenterinfos().remove(infocenterinfo);
			//删除
			getHibernateTemplate().delete(infocenterinfo);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			b=false;
			log.error("delete failed", re);
			throw re;
		}
		return b;
		
	}
	//根据id获得对象
	public Infocenterinfo idShowInfocenterinfo(int id) {
		log.debug("getting Infocenterinfo instance with id: " + id);
		try {
			Infocenterinfo instance = getHibernateTemplate().get(Infocenterinfo.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public Boolean updateCenter(Infocenterinfo infocenterinfo) {
		Boolean b=true;
		log.debug("update Infocenterinfo");
		try {	
			getHibernateTemplate().update(infocenterinfo);
		} catch (RuntimeException re) {
			b=false;
			log.error("find by property name failed", re);
			throw re;
		}
		return b;
	}
	//根据类型id查询的方法
	public List  showAll(int itiid) {
		String HQL="";
		if(itiid>0){
			HQL="from Infocenterinfo as a where a.infotypeinfo.itiid=?";
			List list= getHibernateTemplate().find(HQL,itiid);
			return list;
		}else
		{
			HQL="from Infocenterinfo";
			List list= getHibernateTemplate().find(HQL);
			return list;
		}	
	}
	
	//根据标题查询
	public List<Infocenterinfo> showInfocenterinfo(String icititle){
		String HQL="from Infocenterinfo as a where a.icititle like ?";
		Object[] value={"%"+icititle+"%"};
		List list= getHibernateTemplate().find(HQL,value);
		return list;
	}

	public List<Infocenterinfo> idshowPageModelList(int itiid, final int pageNumber,
			final int limit) {
		//final Object[] values
				log.debug("showPageModelList"); 
				final StringBuffer hql = new StringBuffer("from Infocenterinfo as a ");
				if(itiid>0) {
					hql.append("where infotypeinfo.itiid ="+itiid);
				}
				hql.append(" order by icitime desc");
				System.out.println("-----------"+hql.toString());
				List list = getHibernateTemplate().executeFind(new HibernateCallback() {

					public Object doInHibernate(org.hibernate.Session session)
							throws HibernateException, SQLException {
						 	Query query = session.createQuery(hql.toString());
						    //这个用于扩展多条件查询用到的
						   /* for (int i = 0; i < values.length; i++) {
						     query.setParameter(i, values[i]);
						    }*/
						    int p = pageNumber;
						    int l = limit;
						    int a = (p-1)*l;
						    List result = query.setFirstResult(a)
						        .setMaxResults(l)
						        .list();
						    /*List result = query
							        .list();*/
						    System.out.println("需求dao中查询到了数据"+result);
						    return result;
					}
				});
				
				return list;
	}
}